package daobeauty.interceptor.invoke.parser.operator;

import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import daobeauty.util.Stack;

public class AndOperator implements Operator {
	private Operator left;
	private Operator right;
	
	public AndOperator(Operator left, Operator right){
		this.left = left;
		this.right = right;
	}
	
	public Criterion createCriterion(Stack argumentStack) {
		return Restrictions.and(left.createCriterion(argumentStack), right.createCriterion(argumentStack));
	}
	
	public String toHqlQuery(Stack argumentStack, List usedArguments){
		return "("+left.toHqlQuery(argumentStack, usedArguments)+") AND ("+right.toHqlQuery(argumentStack, usedArguments)+")";
	}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((left == null) ? 0 : left.hashCode());
		result = prime * result + ((right == null) ? 0 : right.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final AndOperator other = (AndOperator) obj;
		if (left == null) {
			if (other.left != null)
				return false;
		} else if (!left.equals(other.left))
			return false;
		if (right == null) {
			if (other.right != null)
				return false;
		} else if (!right.equals(other.right))
			return false;
		return true;
	}

	public int getAmountArguments() {
		return 0;
	}

	public int getTotalAmountArguments() {
		return left.getTotalAmountArguments()+right.getTotalAmountArguments();
	}

	public String toTotalString() {
		return "("+left.toTotalString()+") AND ("+right.toTotalString()+")";
	}
	
	public String toString(){
		return toTotalString();
	}
}
